home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / misc / dnstools.shar / findip < prev    next >
Encoding:
Text File  |  1996-10-25  |  992 b   |  43 lines

  1. #!/usr/local/bin/perl
  2. # program to find the next unallocated IP address in a
  3. # Class C network by reading the named.hosts file, given the Class C
  4. # network and the starting address.
  5. #
  6. # Eric Murray 1/21/93
  7. # ericm@microunity.com
  8.  
  9.  
  10. sub usage {
  11.     print "usage: $0 128.2.30.160\n";
  12.     print "prints next free IP address on the subnet, from named.hosts\n";
  13.     exit;
  14. }
  15.  
  16. &usage if ($#ARGV != 0);
  17. @buf = split('\.',$ARGV[0]);
  18.  
  19. # network class dependent:
  20. # this is for Class C.
  21. $netpart = "$buf[0].$buf[1].$buf[2]";
  22. $hostpart = "$buf[3]";
  23.  
  24. open(FH,"named.hosts") || die "can't open named.hosts: $!\n";
  25. while(<FH>) {
  26.     chop;
  27.     s/;.*$//;
  28.     next if (/^$/);
  29.     next if ($_ !~ /\s+A\s+\d+/);    # skip all but A records
  30.     if ($_ =~ /$netpart/) {
  31.         s/^.*A\s+//;
  32.         # it's the same subnet
  33.         $host = $_; $host =~ s/^$netpart\.//;
  34.         next if ($host < $hostpart);
  35.         $nums[$host]++;        # mark down.
  36.     }
  37. }
  38. foreach ($hostpart .. 255) {
  39.     if (!defined $nums[$_]) { print "$netpart.$_\n"; exit; }
  40. }
  41. print "no IP addresses left\n";
  42.             
  43.